package com.tuantuan.common.jdbc;

import java.sql.*;

/**
 * @author: wangqinmin
 * @date : 2020/8/28
 * @description: 仰天大笑出门去，我辈岂是蓬蒿人
 */
public class JdbcUtils {


//    @Value("${spring.datasource.url}")
//    public static String URL;
    public static String URL="jdbc:mysql://47.96.100.61:33061/yuanyuan?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";

//    @Value("${spring.datasource.username}")
//    public static String USER;
    public static String USER = "root";

//    @Value("${spring.datasource.password}")
//    public static String PASSWORD;
    public static String PASSWORD="wangqinmin";

    private static Connection conn = null;

    static {
        try {
            //1.加载驱动程序  //
//            Class.forName("com.mysql.jdbc.Driver");
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2. 获得数据库连接
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        return conn;
    }

    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) throws SQLException {
        //获取连接
        Connection conn = JdbcUtils.getConnection();
        //sql
        String sql = "select * from t_user";
        //预编译
        PreparedStatement ptmt = conn.prepareStatement(sql);
        //执行
        ResultSet rs = ptmt.executeQuery();


        while (rs.next()){
            String courseId = rs.getString("id");
            String courseName = rs.getString("t_name");
            System.out.println(courseId);
            System.out.println(courseName);
        }

        String sql2 = "ALTER TABLE `t_user` ADD COLUMN `phone1` VARCHAR ( 32 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '电话'";
        PreparedStatement ptmt1 = conn.prepareStatement(sql2);
        boolean execute = ptmt1.execute();
        System.out.println(execute);
    }
//
//    public static boolean alter(DataBaseAlter dataBaseAlter) throws SQLException {
//        String sql = "ALTER TABLE `"+dataBaseAlter.getTableName()+"` ADD COLUMN `"+dataBaseAlter.getField()+"` VARCHAR ( 32 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '"+dataBaseAlter.getComment()+"'";
//        PreparedStatement ptmt = conn.prepareStatement(sql);
//        return ptmt.execute();
//    }
}
